﻿package com.tangchao.client.sdh.sdhinterface
{
	import com.smartfoxserver.v2.entities.SFSUser;
	import com.tangchao.client.entities.User;
	import com.tangchao.client.sdh.control.SanDaHaControl;
	import com.tangchao.client.sdh.definition.DFState;

	public class Test
	{
		private var _in:In;
		private var _out:Out;
		
		private var _userArr:Array = new Array;
		private var _readyArr:Array = new Array;
		
		static public var factor:int = 0;
		static private var _instance:Test;
		
		public function Test()
		{
			_in  = In.getInstance();
			_out = Out.getInstance();
			_instance = this;
		}
		
		static public function getInstance():Test
		{
			if(!_instance)
			{
				_instance = new Test();
			}
			return _instance;
		}
		
		public function doTest(typeVal:String, param:String, param2:String):void
		{
			var type:int = parseInt(typeVal);
			
			if(type == 0)
			{	
				SanDaHaControl.getInstance().setMsg("请设置测试类型");
				return;
			}
			
			switch(type)
			{
				case 1:
					doUserEnterRoom(param, param2);
					break;
				case 2:
					doUserLeaveRoom(param, param2);
					break;
				case 3:
					doUserReady(param, param2);
					break;
				case 4:
					doFapai(param, param2);
					break;
				case 5:
					doJiaofen(param, param2);
					break;
				case 6:
					doSelectMainCard(param, param2);
					break;
				case 7:
					doPutBottomDown(param, param2);
					break;
				case 8:
					doUserPlayCard(param, param2);
					break;
				case 9:
					doUserDao(param, param2);
					break;
				case 10:
					doUserGen(param, param2);
					break;
				case 11:
					doUserFan(param, param2);
					break;
				case 12:
					doHideDaoGenFan(param, param2);
					break;
				case 13:
					doUserTuoGuan(param, param2);
					break;
				case 14:
					doEnterSetMode();
					break
				case 15:
					doUserSetScoreModel(param, param2);
					break;
				default:
					SanDaHaControl.getInstance().setMsg("未知的测试类型");
					break;
			}
		}
		
		private function createUser(name:String, seatNO:int):User
		{
			if(seatNO<0 || seatNO>3)
			{
				trace(this, "创建角色失败 座位非法");
				SanDaHaControl.getInstance().setMsg("创建角色失败 座位非法");
				return null;
			}
			var sfsUser:SFSUser = new SFSUser(++factor, name, true);
			var user:User = new User(sfsUser);
			user.setCredit(100);
			user.setExperience(1000);
			user.setGender(0);
			user.setLevel(10);
			user.setSeatNo(seatNO);
			user.setVigour(78);
			return user;
		}
		
		//1：进入房间 param-玩家名字 param2-座位号
		private function doUserEnterRoom(param:String, param2:String):void
		{
			var seatNo:int = parseInt(param2);
			_userArr.push(createUser(param, seatNo));
			
			_in.userEnterRoom(param, seatNo, _userArr);
			
		}
		
		
		//2-玩家离开房间 param-名字
		private function doUserLeaveRoom(param:String, param2:String):void
		{
			var i:int;
			var user:User;
			var userName:String;
			var newArr:Array = new Array;
			
			for(i=0; i<_userArr.length; i++)
			{
				user = _userArr[i] as User;
				if(user && user.getName() != param)
				{
					newArr.push(user);
				}
			}
			_userArr = newArr;
			
			newArr = new Array;
			
			for(i=0; i<_readyArr.length; i++)
			{
				userName = _readyArr[i] as String;
				if(userName != param)
				{
					newArr.push(user);
				}
			}
			_readyArr = newArr;
			_in.userLeaveRoom(param, _userArr);
		}
		
		//3用户准备 param-准备玩家的名字
		public function doUserReady(param:String, param2:String):void
		{
			var user:User;
			var i:int;
			var isValid:Boolean = false;
			
			for(i=0; i<_userArr.length; i++)
			{
				user = _userArr[i] as User;
				if(user && user.getName() == param)
				{
					isValid = true;
					_readyArr.push(param);
					break;
				}
			}
			
			if(!isValid)
			{
				SanDaHaControl.getInstance().setMsg("无效的用户名:"+param);
			}
			else
			{
				if(_readyArr.length==4)
				{
					_in.userReady(param, _readyArr, true);
				}
				else
				{
					_in.userReady(param, _readyArr, false);
				}
			}
		}
		
		//4：发牌 param：下一个叫分的用户 param2-能叫的最大分数
		private function doFapai(param:String, param2:String):void
		{
			var paiArr:Array = new Array(1,2,8,24,35,36, 37, 42,44,45);
			
			_in.fapai(paiArr, param, parseInt(param2), true);
		}
		
		//5:叫分 param:叫分的玩家  param2:0-叫分结束 1-叫分为结束
		private function doJiaofen(param:String, param2:String):void
		{			
			var param2v:int = parseInt(param2);
			var arrTmp:Array;
			var arrParam:Array;
			var dipaiArr:Array;
			
			arrTmp = new Array(3,5,8,11,22,23,24,32,35, 37, 42,44,45, 46);
			dipaiArr= new Array(3,5,8,11,22,23,24);
			if(param2v)
			{
				arrParam = new Array;
				arrParam.push("c");  //庄家的用户名
				arrParam.push(dipaiArr);
				arrParam.push(arrTmp);
				_in.userBet(param, -1, false, arrParam);
			}
			else
			{
				arrParam = new Array;
				arrParam.push("c"); //下一个叫分玩家的用户名
				arrParam.push(50);
				_in.userBet(param, 60, true, arrParam);
			}
		}
		
		//6：选择主牌 param:庄家名字 param2:花色(1-方块 2-红桃 3-草花 4-黑桃)
		private function doSelectMainCard(param:String, param2:String):void
		{
			var mianCard:int = parseInt(param2);
			_in.userClaimMainCard(param, mianCard);
		}
		
		//7:下底牌 param:下主牌玩家名字
		private function doPutBottomDown(param:String, param2:String):void
		{
			var cardArr:Array = new Array(1,2,8);
			_in.userPutBottomCards(param, cardArr);
		}
		
		//8 param:出牌玩家 param2:0-此轮未结束 1-此轮结束 2-此局结束
		private function doUserPlayCard(param:String, param2:String):void
		{
			var cardArr:Array = new Array(2,4,5,6,6,7,8,11);
			var stat:int = parseInt(param2);
			var paramArr:Array = new Array;
			var subArr:Array = new Array("c");
			
			trace(this, "doUserPlayCard");
			switch(stat)
			{
				case 0:
				case 1:
					paramArr.push(param);
					_in.userPlayCards(param, cardArr, true, stat, paramArr);
					break;
				case 2:
					SanDaHaControl.getInstance().setMsg("结算接口已修改");
					paramArr.push(subArr);
					paramArr.push("test1");
					paramArr.push(cardArr);
					paramArr.push( new Array(10, 1, 2));
					paramArr.push("c");
					paramArr.push(cardArr);
					paramArr.push( new Array(10, 1, 2));
					paramArr.push("test3");
					paramArr.push(cardArr);
					paramArr.push( new Array(10, 1, 2));
					paramArr.push("test4");
					paramArr.push(cardArr);
					paramArr.push(new Array(10, 1, 2));
					paramArr.push(0);
					paramArr.push(80);
					paramArr.push(1);
					paramArr.push(75);//得分数
					paramArr.push(75);//叫分数
					_in.userPlayCards(param, cardArr, true, stat, paramArr);
					break;
				default:
					SanDaHaControl.getInstance().setMsg("错误的牌局状态");
					break;	
			}
		}
		
		//9 param:userName
		private function doUserDao(param:String, param2:String):void
		{
			_in.userDao(param);
		}
		
		//10 param:userName
		private function doUserGen(param:String, param2:String):void
		{
			_in.userGen(param);
		}
		
		//11 param:userName
		private function doUserFan(param:String, param2:String):void
		{
			_in.userFan(param);
		}
		
		//12 param:1-倒 2-跟 3-反 param2：0-隐藏 1-显示 
		private function doHideDaoGenFan(param:String, param2:String):void
		{
			var type:int = parseInt(param);
			var val:int  = parseInt(param2);
			
			if(type==1 && val==0)
			{
				_in.disableDaoBtn(true);
				return;
			}
			
			if(type==1 && val!=0)
			{
				_in.disableDaoBtn(false);
				return;
			}
			
			if(type==2 && val==0)
			{
				_in.disalbeGenBtn(true);
				return;
			}
			
			if(type==2 && val!=0)
			{
				_in.disalbeGenBtn(false);
				return;
			}
			
			if(type==3 && val==0)
			{
				_in.disableFanBtn(true);
				return;
			}
			
			if(type==3 && val!=0)
			{
				_in.disableFanBtn(false);
				return;
			}
		}
		
		//13 param:userName param2:0-托管 1-取消托管
		private function doUserTuoGuan(param:String, param2:String):void
		{
			var val:int = parseInt(param2);
			
			if(val==0)
			{
				_in.userTuoGuan(param, true);
			}
			else
			{
				_in.userTuoGuan(param, false);
			}
		}
		
		//14
		private function doEnterSetMode():void
		{
			_in.enterSetMode();
		}
		
		//15 param：param:0-普通 1-心跳 param2：分数
		private function doUserSetScoreModel(param:String, param2:String):void
		{
			var mode:int = parseInt(param);
			var score:int= parseInt(param2);
			_in.userSetScoreModel("c", mode, score, "c", true);
		}
		
		
	}
}